package com.zhongke.cotrun.controller;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.ExpertInfo;
import com.zhongke.cotrun.model.pojo.UserInfo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.model.resultpackage.ReturnResult;
import com.zhongke.cotrun.model.validator.UserValidator;
import com.zhongke.cotrun.service.ExpertInfoService;
import com.zhongke.cotrun.service.UserInfoService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Type;

@Api(tags = "专家接口")
@RestController
@RequestMapping("/ExpertInfo")
@Validated
public class ExpertInfoController {
    @Autowired
    ExpertInfoService expertInfoService;
    @Autowired
    UserInfoService userInfoService;

    @ApiOperation(value = "专家注册接口", httpMethod = "POST", response = ReturnResult.class)
    /**
     * 添加专家
     * @param expertInfo
     * @return
     */
    @PostMapping("/addExperInfo")
    public APIResponse addExperInfo(ExpertInfo expertInfo, @ApiIgnore HttpSession session) throws MyException {
        UserInfo userInfo = expertInfo.getUserInfo();
        String type = userInfo.getType();
        if (type == null || !"1".equals(type)) {
            throw new MyException(CodeMsg.TYPE_ERROR);
        }

        if (expertInfoService.addExperInfo(expertInfo, session)) {
            return APIResponse.returnSuccess();
            // return ReturnResult.success();
        }
        return APIResponse.returnFail(CodeMsg.ADD_ERROR);
        //  return  ReturnResult.erro(CodeMsg.ADD_ERROR);
    }


    /**
     * 按条件查询专家列表
     *
     * @param userInfo
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "专家按条件检索接口", httpMethod = "POST", response = ReturnResult.class)
    @PostMapping("/selectExperInfoByCondition")
    public APIResponse selectExperInfoByCondition(UserInfo userInfo,
                                                  @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                                                  @RequestParam(required = false, defaultValue = "12") Integer pageSize) {


        return APIResponse.returnSuccess(expertInfoService.selectExpertInfoByCondition(userInfo, pageNumber, pageSize));



    }

    /**
     * 查看具体
     *
     * @param expertId
     * @return expertId具体的
     * linkUserId自己
     */
    @ApiOperation(value = "查询自己的或者具体的检索接口")
    @GetMapping("/selectExperById")
    public APIResponse selectExperById(
            @ApiParam(name = " expertId", value = "专家信息id", required = true)
            @NotNull(message = "专家id不能为空") Integer expertId) {
        ExpertInfo expertInfo = new ExpertInfo();
        expertInfo.setExpertId(expertId);
        return APIResponse.returnSuccess(expertInfoService.selectExpertById(expertInfo));
        // return ReturnResult.success(expertInfoService.selectExpertById(expertInfo));
    }

    /**
     * 查询自己的专家信息接口
     *
     * @return
     */
    @ApiOperation(value = "查询自己的信息检索接口", httpMethod = "POST", response = ReturnResult.class)
    @RequestMapping("/selectMyExperInfo")
    public APIResponse selectMyExperInfo(HttpSession session) throws MyException {
//        UserInfo userInfo = (UserInfo) session.getAttribute("USERINFO");
//        if (userInfo == null) {
//            throw new MyException(CodeMsg.MUST_LOGIN_ERROP);
//        }

//        ExpertInfo expertInfo = new ExpertInfo();
//     expertInfo.setLinkUserId(userInfo.getUserId());
        return APIResponse.returnSuccess(expertInfoService.selectMyExpertInfo(ShiroUtils.getWebUserId().intValue()));
        // return ReturnResult.success(expertInfoService.selectExpertById(expertInfo));
    }


}
